<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Logic;

/**
 * 小哥业务逻辑层
 *
 * @author derek <693472824@qq.com>
 */
class WorkerLogic extends BaseLogic {

    /**
     * 根据小哥ID获取小哥
     * @param   integer $roid 小哥ID
     * @author  derek <693472824@qq.com>
     */
    public function getWorkerByID ($workerID) {
        
        if(empty($workerID) || !is_numeric($workerID)){
            return null;
        }
        
        $view = D('WorkerSiteView');
        $map['worker_id'] = (string)$workerID;
        $worker = $view->where($map)->find();
        return $worker;
    }
    
    /**
     * 保存小哥信息
     *
     * @author derek <693472824@qq.com>
     */
    public function saveAction() {
        
        $workerID = I('worker_id');
        $workerModel = D('Worker');
    
        $data = I();
        $data['profession'] = is_array($data['skill']) ? implode(',', $data['skill']) : '';
        
        if ($workerModel->create($data)) {
    
            if (empty($workerID)) {
                
                return $workerModel->add();
            } else {
                
                $map['worker_id'] = $workerID;
                
                $result = $workerModel->where($map)->save();
                
                $status = I('status');
                
                if($status == '3' && $result !== false){
                    
                    $trainingTime = I('training_time');
                    $trainingID = I('training_id');
                    
                    $trainingList = D('Training', 'Logic')->getTrainingList();
                    
                    if($trainingTime && $trainingList[$trainingID]){
                        
                        $message = C('TRAINING_MESSAGE') ? C('TRAINING_MESSAGE') : '短信模板';
                        
                        $message = str_replace('#training_time#', $trainingTime, $message);
                        $message = str_replace('#training_address#', $trainingList[$trainingID]['address'], $message);
                        $account = I('account');
                        $result = api('Util/sendCode', array($account, $message));
                        
                        if($result == '1'){
                            $workerModel->where($map)->setField('status', '4');
                        }
                    }
                }elseif ($status == '4'){
                    $workerModel->where($map)->setField('status', '1');
                }
                
                return $result;
            }
        } else {
            $this->error = $workerModel->getError();
            return false;
        }
    }

    /**
     * 小哥删除
     * @param   integer|array 需要删除的小哥ID
     * @return  boolean
     * @author  derek <693472824@qq.com>
     */
    public function del ($workerID) {
        
        if(empty($workerID)){
            return false;
        }
        
        $map['worker_id'] = $workerID;
        $map['status'] = array('in', '2,3,4,5,8,10');          //有未处理订单时不允许删除
        $orderList = M('Orders')->where($map)->select();
        
        if(empty($orderList)){
            
            $workerModel = M('Worker');
            $workerMap['worker_id'] = $workerID;
            $worker = $workerModel->where($workerMap)->find();
            $result = $workerModel->where($workerMap)->delete();
            
            S('token.'.$worker['account'], NULL);
        }else{
            $result = '-1';
        }    
        
        return $result;
    }

}